// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.process.actions;

import java.util.List;

import org.dengues.core.components.EGlobalComponentNames;
import org.dengues.core.process.EConnectionType;
import org.dengues.designer.ui.editors.palette.CompConnectionCreationTool;
import org.dengues.designer.ui.process.models.CompBlock;
import org.dengues.designer.ui.process.models.CompBlockExit;
import org.dengues.designer.ui.process.models.CompNode;
import org.dengues.designer.ui.process.parts.AbstractProcessPart;
import org.dengues.designer.ui.process.parts.CompNodePart;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.gef.requests.CreationFactory;
import org.eclipse.gef.ui.actions.SelectionAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Event;
import org.eclipse.ui.IWorkbenchPart;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * @since 2007-10-9 下午10:09:35
 */
public class ConnectionCreateAction extends SelectionAction {

    public static final String ID = "org.dengues.designer.ui.process.actions.ConnectionCreateAction"; //$NON-NLS-1$

    private AbstractProcessPart nodePart;

    private EConnectionType type;

    private String unName;

    /**
     * Qiang.Zhang.Adolf@gmail.com ConnectionCreateAction constructor comment.
     * 
     * @param part
     */
    public ConnectionCreateAction(IWorkbenchPart part, EConnectionType type) {
        super(part);
        setId(ID + type.toString());
        setText(type.getDisplayName());
        this.type = type;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com ConnectionCreateAction constructor comment.
     * 
     * @param part
     * @param type
     * @param displayName
     */
    public ConnectionCreateAction(IWorkbenchPart part, EConnectionType type, String displayName) {
        this(part, type);
        setText(displayName);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.ui.actions.SelectionAction#getSelection()
     */
    @Override
    public ISelection getSelection() {
        return super.getSelection();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.ui.actions.SelectionAction#setSelection(org.eclipse.jface.viewers.ISelection)
     */
    @Override
    public void setSelection(ISelection selection) {
        super.setSelection(selection);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.ui.actions.WorkbenchPartAction#calculateEnabled()
     */
    @Override
    protected boolean calculateEnabled() {
        if (getSelectedObjects().isEmpty()) {
            return false;
        }
        List parts = getSelectedObjects();
        if (parts.size() == 1) {
            Object o = parts.get(0);
            if (o instanceof CompNodePart) {
                nodePart = (CompNodePart) o;
                if ((nodePart.getModel() instanceof CompNode)) {
                    CompNode node = (CompNode) nodePart.getModel();
                    if (type.equals(EConnectionType.TRG_OK)) {
                        return true;
                    }
                    boolean equals = EGlobalComponentNames.ZMAPPER.getName().equals(node.getCompName())
                            && type.equals(EConnectionType.MAIN);
                    return equals || node.isOutput(type.name());
                }
                if ((nodePart.getModel() instanceof CompBlockExit) || (nodePart.getModel() instanceof CompBlock)) {
                    return true;
                }
            }
        }
        return false;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.action.Action#run()
     */
    @Override
    public void run() {
        if (nodePart == null) {
            return;
        }

        /**
         * Create a mouse down event that thinks it is over the blob and dispatch it. This is a bit of a fudge to mimic
         * what the user ought to do.
         */

        Point point = null;
        point = nodePart.getFigure().getClientArea().getCenter();
        Point p = point;
        nodePart.getFigure().translateToAbsolute(p);

        Canvas canvas = (Canvas) nodePart.getViewer().getControl();
        Event event = new Event();
        event.button = 1;
        event.count = 0;
        event.detail = 0;
        event.end = 0;
        event.height = 0;
        event.keyCode = 0;
        event.start = 0;
        event.stateMask = 0;
        event.time = 9516624; // any old time... doesn't matter
        event.type = 3;
        event.widget = canvas;
        event.width = 0;
        event.x = p.x + 3;
        event.y = p.y + 3;
        CreationFactory factory = new CreationFactory() {

            public Object getNewObject() {
                return unName;
            }

            public Object getObjectType() {
                return type;
            }

        };
        CompConnectionCreationTool tool = new CompConnectionCreationTool(factory);
        nodePart.getViewer().getEditDomain().setActiveTool(tool);
        canvas.notifyListeners(3, event);
    }

    /**
     * Sets the unName.
     * 
     * @param unName the unName to set
     */
    public void setUnName(String unName) {
        this.unName = unName;
    }

}
